/*
	Coordinator.m
	by Joe Freeman, David LaVallee
	Subprocess Example, Release 2.0
	NeXT Computer, Inc.

	You may freely copy, distribute and reuse the code in this example.
	NeXT disclaims any warranty of any kind, expressed or implied, as to
	its fitness for any particular use.
*/

#import "Subprocess.h"
#import "Coordinator.h"
#import "CommandScroll.h"
#import <stdio.h>
#import <appkit/nextstd.h>
#import <appkit/Application.h>
#import <appkit/Panel.h>
#import <objc/NXStringTable.h>
#import <strings.h>
#import <appkit/Form.h>
#import "Instrum.h"

@implementation Coordinator

id Inst = nil; 

+ initialize
{
	Inst = [[Instrum alloc] init];
	return self;
}

- subprocessOutput:(char *)buffer
{
    [commandView appendString:buffer];
    return self;
}
/*===============================================================*/

- subprocessDone
{ 
    [NXApp terminate:self];
    return self;
}

- subprocessError:(const char *)errorString
    // uses stringTable to localize error messages as appropriate,
    // and then displays the message in an alert panel
{
    const char *returnedString;
    
    if (returnedString = [stringTable valueForStringKey:errorString])
    {
	NXRunAlertPanel(0, returnedString, 0, 0, 0);
    }
    else
    {
	NXRunAlertPanel(0, errorString, 0, 0, 0);
    }
    return self;
}

- writeCode:(char *)buffer
{
	char instDir[80];
	char instFile[80];
	char cmdDir[80];
	char cmixDir[80];
	
	strcpy(instFile,[instName stringValueAt:0]);
	strcpy(instDir,[instPath stringValueAt:0]);
	strcpy(cmdDir,[pathNames stringValueAt:4]);
	strcpy(cmixDir,[pathNames stringValueAt:5]);
	//printf("coordinator: writeCode\n");
	[Inst writeCodeWithName:instFile andDir:instDir andCmd:cmdDir andCmix:cmixDir];
	return self;
}

- compileInst:(char *)buffer
{
	char str[300];
	char instDir[80];
	
	strcpy(instDir,[instPath stringValueAt:0]);
	sprintf(str,"cd %s;make", instDir);
	//printf("str: %s\n",str);
    theSubprocess =
	[[Subprocess alloc]
	    init:str withDelegate:self andPtySupport:YES andStdErr:YES];
    return self;

}
- runIt:(char *)buffer
{
	char str[300];
	char scoreFile[80];
	char instDir[80];
	char instFile[80];
	char soundFile[80];
	char soundDir[80];
	char scoreDir[80];
	char cmdDir[80];
	long sr;
	int  nchans;
	
	
	strcpy(scoreFile,[pathNames stringValueAt:2]);
	strcpy(scoreDir,[pathNames stringValueAt:3]);
	strcpy(soundFile,[pathNames stringValueAt:0]);
	strcpy(soundDir,[pathNames stringValueAt:1]);
	strcpy(instFile,[instName stringValueAt:0]);
	strcpy(instDir,[instPath stringValueAt:0]);
	strcpy(cmdDir,[pathNames stringValueAt:4]);
	if([sampRate selectedRow] == 0)
		sr = 44100;
	else
		sr = 22050;
	nchans = [numChans selectedRow] + 1;
	sprintf(str,"%s/sfcreate -r %ld -c %d -f %s/%s;%s/%s < %s/%s.sco", cmdDir,sr,nchans,soundDir,soundFile,instDir,instFile,scoreDir,scoreFile);
	//printf("str: %s\n",str);
    theSubprocess =
	[[Subprocess alloc]
	    init:str withDelegate:self andPtySupport:YES andStdErr:YES];
    return self;

}
- rescaleIt:(char *)buffer
{
	char str[200];
	char soundFile[100];
	char soundDir[80];
	char cmdDir[80];
	
	strcpy(soundFile,[pathNames stringValueAt:0]);
	strcpy(soundDir,[pathNames stringValueAt:1]);
	strcpy(cmdDir,[pathNames stringValueAt:4]);
	sprintf(str,"%s/rescale %s/%s", cmdDir, soundDir, soundFile);
	//printf("str: %s\n",str);
    theSubprocess =
	[[Subprocess alloc]
	    init:str withDelegate:self andPtySupport:YES andStdErr:YES];
    return self;

}
 
- playIt:(char *)buffer
{
	char str[200];
	char soundFile[100];
	char soundDir[80];
	char cmdDir[80];
	
	strcpy(soundFile,[pathNames stringValueAt:0]);
	strcpy(soundDir,[pathNames stringValueAt:1]);
	strcpy(cmdDir,[pathNames stringValueAt:4]);
	sprintf(str,"%s/play %s/%s.short", cmdDir, soundDir, soundFile);
	//printf("str: %s\n",str);
    theSubprocess =
	[[Subprocess alloc]
	    init:str withDelegate:self andPtySupport:YES andStdErr:YES];
    return self;

}

- writeScore:(char *)buffer
{
	int i;
	char p[17][10];
	char scoreFile[80];
	char scoreDir[80];
	char scofile[160];
	char soundFile[80];
	char soundDir[80];
	char inst[80];
	FILE *fp;
		
	strcpy(soundFile,[pathNames stringValueAt:0]);
	strcpy(soundDir,[pathNames stringValueAt:1]);
	strcpy(scoreFile, [pathNames stringValueAt:2]);
	strcpy(scoreDir, [pathNames stringValueAt:3]);
	strcpy(inst, [instName stringValueAt:0]);
	sprintf(scofile, "%s/%s.sco", scoreDir, scoreFile);
	for(i = 0; i < 17; i++) {
		strcpy(p[i],[scoreData stringValueAt:i]);
		//printf("%s %d %s\n",scofile,i,p[i]);
	}

	fp = fopen(scofile,"w");
	if(!fp) {
		NXRunAlertPanel("write score:","This file can't be opened. Check out the directory. %s","OK",NULL,NULL,scofile);
		return 0;
	}
	fprintf(fp,"output(\"%s/%s\",0)\n",soundDir,soundFile);
	fprintf(fp,"sfclean(0)\n");
	fprintf(fp,"makegen(1,10,1024,1)\n"); 		// sine
	fprintf(fp,"makegen(2,7,512,0,512,1)\n");	// linear
	fprintf(fp,"makegen(3,5,512,.01,512,1)\n");	// exponential
	fprintf(fp,"%s(%s",inst,p[0]);
	for(i = 1; i < 17; i++) {
		if(strcmp("",p[i]))
			fprintf(fp,", %s",p[i]);
	}
	fprintf(fp,")\n");

	fclose(fp);
	return self;
}
@end
